package cn.ycc1.boot3oom.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @author ycc
 * @date 2024/7/27
 * https://blog.csdn.net/kkoyy459/article/details/126914555
 * -Xms 设置初始化内存分配大小
 * -Xmx 设置最大分配内存
 * -XX:+PrintGCDetails // 打印GC垃圾回收信息
 * -XX:+HeapDumpOnOutOfMemoryError // oom dump
 * -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
 */
@RestController
public class TestController {
    private class Demo{}

    @GetMapping("/test")
    public String test() {
        ArrayList<Demo> list = new ArrayList<>();

        int count = 0;
        try {
            while (true){
                list.add(new Demo());
                count = count + 1;
            }
        }catch (Error e){ // OOM是错误，不是异常，所以这里是捕获Error
            System.out.println("count:" + count);
            e.printStackTrace();
        }

        return "test";
    }
}
